const Service = require('egg').Service;

class AppService extends Service {

  // 初始化角色
  async initRole() {
    const { app, app: { model: { Role } } } = this;

    await Role.truncate()

    await Role.create({
      id: 1,
      role_name: '总经办',
    });
    await Role.create({
      id: 2,
      role_name: '技术部',
    });
    await Role.create({
      id: 3,
      role_name: '营销中心',
    });
    await Role.create({
      id: 4,
      role_name: '行政部',
    });
    await Role.create({
      id: 5,
      role_name: '产品方案部',
    });
    await Role.create({
      id: 6,
      role_name: '供应链部',
    });
  }

  // 初始化权限
  async initPower() {
    const { app, app: { model: { RolePower, Role } } } = this;

    await RolePower.truncate();

    // 售后模块权限
    const powers = [
      { power_code: 'SH01', power_text: '指派' },
      { power_code: 'SH02', power_text: '接单&z转发' },
      { power_code: 'SH03', power_text: '处理&签到' },
      { power_code: 'SH04', power_text: '评价' },
      { power_code: 'SH05', power_text: '查看评价' },
      { power_code: 'SH06', power_text: '归档' },
    ];

    // 查询现有角色
    const roles = await Role.findAll();

    for (let role of roles) {
      for (let power of powers) {
        await RolePower.create({
          role_id: role.id,
          power_code: power.power_code,
          power_text: power.power_text,
        });
      }
    }

  }

  //初始化字典类型
  async initDict() {
    const { app, app: { model: { Dictionary } } } = this
    await Dictionary.truncate()
    //客户来源
    Dictionary.create({
      id: '10000000-0000-0000-0000-000000000001',
      name: '400热线',
      value: 1,
      type: '客户来源'
    })
    Dictionary.create({
      id: '10000000-0000-0000-0000-000000000002',
      name: '线下',
      value: 2,
      type: '客户来源'
    })
    Dictionary.create({
      id: '10000000-0000-0000-0000-000000000003',
      name: '展会',
      value: 3,
      type: '客户来源'
    })
    //客户类型
    Dictionary.create({
      id: '20000000-0000-0000-0000-000000000001',
      name: '原始客户',
      value: 1,
      type: '客户类型'
    })
    Dictionary.create({
      id: '20000000-0000-0000-0000-000000000002',
      name: '潜在客户',
      value: 2,
      type: '客户类型'
    })
    Dictionary.create({
      id: '20000000-0000-0000-0000-000000000003',
      name: '意向客户',
      value: 3,
      type: '客户类型'
    })
    Dictionary.create({
      id: '20000000-0000-0000-0000-000000000004',
      name: '重点客户',
      value: 4,
      type: '客户类型'
    })
    Dictionary.create({
      id: '20000000-0000-0000-0000-000000000005',
      name: 'VIP客户',
      value: 5,
      type: '客户类型'
    })
    //客户性质
    Dictionary.create({
      id: '30000000-0000-0000-0000-000000000001',
      name: '业主',
      value: 1,
      type: '客户性质'
    })
    Dictionary.create({
      id: '30000000-0000-0000-0000-000000000002',
      name: '工程商',
      value: 2,
      type: '客户性质'
    })
    Dictionary.create({
      id: '30000000-0000-0000-0000-000000000003',
      name: '运营商',
      value: 3,
      type: '客户性质'
    })
    Dictionary.create({
      id: '30000000-0000-0000-0000-000000000004',
      name: '中间人',
      value: 4,
      type: '客户性质'
    })
    //初步结果
    Dictionary.create({
      id: '40000000-0000-0000-0000-000000000001',
      name: '需求明确',
      value: 1,
      type: '初步结果'
    })
    Dictionary.create({
      id: '40000000-0000-0000-0000-000000000002',
      name: '已发标准方案',
      value: 2,
      type: '初步结果'
    })
    Dictionary.create({
      id: '40000000-0000-0000-0000-000000000003',
      name: '已发报价',
      value: 3,
      type: '初步结果'
    })
    //合作方式
    Dictionary.create({
      id: '50000000-0000-0000-0000-000000000001',
      name: '代理需求',
      value: 1,
      type: '合作方式'
    })
    Dictionary.create({
      id: '50000000-0000-0000-0000-000000000002',
      name: '项目合作',
      value: 2,
      type: '合作方式'
    })
    //过保状态
    Dictionary.create({
      id: '60000000-0000-0000-0000-000000000001',
      name: '不确定',
      value: 0,
      type: '过保状态'
    })
    Dictionary.create({
      id: '60000000-0000-0000-0000-000000000002',
      name: '在保',
      value: 1,
      type: '过保状态'
    })
    Dictionary.create({
      id: '60000000-0000-0000-0000-000000000003',
      name: '过保',
      value: 2,
      type: '过保状态'
    })
    //性别
    Dictionary.create({
      id: '70000000-0000-0000-0000-000000000001',
      name: '保密',
      value: 0,
      type: '性别'
    })
    Dictionary.create({
      id: '70000000-0000-0000-0000-000000000002',
      name: '男',
      value: 1,
      type: '性别'
    })
    Dictionary.create({
      id: '70000000-0000-0000-0000-000000000003',
      name: '女',
      value: 2,
      type: '性别'
    })
    //故障类型
    Dictionary.create({
      id: '80000000-0000-0000-0000-000000000001',
      name: '软件故障',
      value: 1,
      type: '故障类型'
    })
    Dictionary.create({
      id: '80000000-0000-0000-0000-000000000002',
      name: '硬件故障',
      value: 2,
      type: '故障类型'
    })
    Dictionary.create({
      id: '80000000-0000-0000-0000-000000000003',
      name: '人为损坏',
      value: 3,
      type: '故障类型'
    })
    //处理结果
    Dictionary.create({
      id: '90000000-0000-0000-0000-000000000001',
      name: '已解决',
      value: 1,
      type: '处理结果'
    })
    Dictionary.create({
      id: '90000000-0000-0000-0000-000000000002',
      name: '未解决',
      value: 2,
      type: '处理结果'
    })
  }

  //初始化企业信息
  async initCompany() {
    const { app: { model: { Company } } } = this
    await Company.truncate();

    Company.create({
      name: '武汉经纬视通科技有限公司',
      code: 'jinwin',
      corpid: 'ding3a8c9da236345ba135c2f4657eb6378f',
      secret: 'x-DPygCbyLALX9dsV70jLkihwOxHSEH16M_b7c7J7eBk3OClXwWBCCcW0oVE5B7f',
    })
    Company.create({
      name: '武汉清清小溪有限公司',
      code: 'qqxx',
      corpid: 'dingc6f6abc21a0d46b735c2f4657eb6378f',
      secret: 'x-o62WizIR9PC_2Rc4qxHAR3GODK1xo6BpZiA22mgprhg4oqVJbEF3HzjWfnRl0_nF',
    })
  }
}

module.exports = AppService
